//
//  ClassViewController.m
//  MMLOOK
//
//  Created by dllo on 15/8/29.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "ClassViewController.h"
#import "ClassCollectionViewCell.h"
#import "AsynConnect.h"
#import "PresentViewController.h"
#import "MBProgressHUD.h"
#import "SaveTools.h"
#import "JudgeConnection.h"
#import "Alert.h"
#import "ClassifyTVCell.h"
#import "HeaderView.h"

@interface ClassViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *classArray;
@property (nonatomic, retain)NSMutableArray *objectArray;
@property (nonatomic, retain)NSMutableArray *situArray;
@property (nonatomic, retain)NSMutableArray *styleArray;
@property (nonatomic, retain) MBProgressHUD *mbProView;
@property (nonatomic, copy) NSString *url;

@end

@implementation ClassViewController

- (void)dealloc
{
    [_tableView release];
    [_url release];
    [_mbProView release];
    [_classArray release];
    [_objectArray release];
    [_situArray release];
    [_styleArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.url = @"http://api.liwushuo.com/v1/channel_groups/all";
        
        self.classArray = [NSMutableArray array];
        self.objectArray = [NSMutableArray array];
        self.situArray = [NSMutableArray array];
        self.styleArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"更多分类";
    [self createTableView];
    [self readLocalData];
    [self connection];
    self.view.backgroundColor = [UIColor colorWithRed:105./ 255 green:204. / 255 blue:0. / 255 alpha:1];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 104)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [_tableView registerClass:[ClassifyTVCell class] forCellReuseIdentifier:@"123"];
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *str = @"0. 0";
    HeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:str];
    if (!headerView) {
        
        headerView = [[[HeaderView alloc] initWithReuseIdentifier:str] autorelease];
    }
    
    switch (section) {
        case 0:
        {
            headerView.image = @"iconfont-hot@2x.png";
            headerView.text = @"品类";
        }
            break;
        case 1:
        {
            headerView.image = @"iconfont-person@2x.png";
            headerView.text = @"对象";
        }
            break;
        case 2:
        {
            headerView.image = @"iconfont-place@2x.png";
            headerView.text = @"场合";
        }
            break;
        case 3:
        {
            headerView.image = @"iconfont-style@2x.png";
            headerView.text = @"风格";
        }
            break;
            
        default:
            break;
    }
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (self.view.frame.size.height - 160) / 4;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassifyTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    
    switch (indexPath.section) {
        case 0:
        {
            cell.array = [NSMutableArray arrayWithArray:_classArray];
        }
            break;
        case 1:
        {
            cell.array = [NSMutableArray arrayWithArray:_objectArray];
        }
            break;
        case 2:
        {
            cell.array = [NSMutableArray arrayWithArray:_situArray];
        }
            break;
        case 3:
        {
            cell.array = [NSMutableArray arrayWithArray:_styleArray];
        }
            break;
            
        default:
            break;
    }
    
    [cell sendDicWithBlock:^(NSMutableDictionary *dic) {
        
        PresentViewController *presentVC = [[[PresentViewController alloc] init] autorelease];
        presentVC.dic = dic;
        
        switch (indexPath.section) {
            case 0:
            {
                self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"品类" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
            }
                break;
            case 1:
            {
                self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"对象" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
            }
                break;
            case 2:
            {
                self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"场合" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
            }
                break;
            case 3:
            {
                self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"风格" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
            }
                break;
                
            default:
                break;
        }
        
        [self.navigationController pushViewController:presentVC animated:YES];
    }];
    
    return cell;
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.translucent = NO;
}

- (void)connection
{
    [self createMbProView];
    
    [AsynConnect asynConnectWithUrlstr:_url parmater:nil block:^(NSData *data) {
        
        if (data) {
            
            BOOL saveResult = [SaveTools saveDataWithName:[_url hash] directory:NSLibraryDirectory data:data];// 有网络时 数据储存在本地
            NSLog(@"更多分类存储: %d", saveResult);
            
            [_classArray removeAllObjects];
            [_objectArray removeAllObjects];
            [_situArray removeAllObjects];
            [_styleArray removeAllObjects];
            
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
            NSMutableArray *arr = [littleDic objectForKey:@"channel_groups"];
            //class
            NSMutableDictionary *classDic = [arr objectAtIndex:0];
            NSMutableArray *class = [classDic objectForKey:@"channels"];
            [_classArray addObjectsFromArray:class];
            
            //object
            NSMutableDictionary *objectdic = [arr objectAtIndex:1];
            NSMutableArray *object = [objectdic objectForKey:@"channels"];
            [_objectArray addObjectsFromArray:object];
            
            //situation
            NSMutableDictionary *situDic = [arr objectAtIndex:2];
            NSMutableArray *situ = [situDic objectForKey:@"channels"];
            [_situArray addObjectsFromArray:situ];
            
            //style
            NSMutableDictionary *styleDic = [arr objectAtIndex:3];
            NSMutableArray *style = [styleDic objectForKey:@"channels"];
            [_styleArray addObjectsFromArray:style];
            
            [_tableView reloadData];
            
            [_mbProView hide:YES];
        }
    }];
}

- (void)readLocalData
{
    NSData *data = [SaveTools readDataWithName:[_url hash] directory:NSLibraryDirectory];
    
    if (data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littleDic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littleDic objectForKey:@"channel_groups"];
        //class
        NSMutableDictionary *classDic = [arr objectAtIndex:0];
        NSMutableArray *class = [classDic objectForKey:@"channels"];
        [_classArray addObjectsFromArray:class];
        
        //object
        NSMutableDictionary *objectdic = [arr objectAtIndex:1];
        NSMutableArray *object = [objectdic objectForKey:@"channels"];
        [_objectArray addObjectsFromArray:object];
        
        //situation
        NSMutableDictionary *situDic = [arr objectAtIndex:2];
        NSMutableArray *situ = [situDic objectForKey:@"channels"];
        [_situArray addObjectsFromArray:situ];
        
        //style
        NSMutableDictionary *styleDic = [arr objectAtIndex:3];
        NSMutableArray *style = [styleDic objectForKey:@"channels"];
        [_styleArray addObjectsFromArray:style];
        
        [_tableView reloadData];
    }
}

- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self.view addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
